#ifdef _WINDOWS

#ifndef _DEBUG
#define _SECURE_SCL 1
#endif

#include <Windows.h>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>

int main(int argc, char* argv[]);

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR sCmdLine, INT iShowCmd)
{
	std::string cmdline(sCmdLine);
	std::vector<char*> args;

	// Start the arguments with the process file name
	DWORD pID = GetCurrentProcessId();
	HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pID);
	char lpFileName[256];
	DWORD pFileNameSize = 256;
	BOOL result = QueryFullProcessImageName(hProcess, 0, lpFileName, &pFileNameSize);
	if(result == 0)
	{
		return GetLastError();
	}

	args.push_back(lpFileName);

	// Search for spaces and store the commands
	size_t pos = cmdline.find(' ');

	while(pos != cmdline.npos)
	{
		char* cmd = new char[256];
		strcpy(cmd, cmdline.substr(0, pos).c_str());
		args.push_back(cmd);
		cmdline = cmdline.substr(pos + 1);
		pos = cmdline.find(' ');
	}

	// Add that last command if it exists
	if(cmdline.size() > 0)
	{
		char* cmd = (char*)(cmdline.c_str());
		args.push_back(cmd);
	}

	// Convert the vector to an array of character pointers
	char** argv = new char*[args.size()];
	std::copy(args.begin(), args.end(), argv);

	main(args.size(), argv);

	return 0;
}

#endif